<?php

/**
 * goods
 * @version 1.0
 * @package application
 * @subpackage application/controllers/goods/
 */
class goods extends Front_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('goods_model');
        $this->load->model('goods_class_model');
    }

    //产品频道页
    public function index($pid = 1) {
        $this->lists($pid);
    }

    public function lists($pid = 0) {
        $search['attributes']['gc_id'] = $pid;
        // 总数与分页
        $total = $this->goods_model->total(null, $search);
        $data['total'] = $total;
        // 请使用config_item( 'per_page' )获取全局显示条数
        $per_page = config_item('per_page');
        $per_page = 10;
        $this->load->library('pagination');
        $pagination_config = array(
            'base_url' => base_url($this->_site_path . '/goods/index/' . $pid),
            'total_rows' => $total,
            'per_page' => $per_page,
            'uri_segment' => 5,
        );
        $pagination_config = array_merge($pagination_config, $this->pagination_config);
        //分页
        $this->pagination->initialize($pagination_config);
        $data['pagination'] = $this->pagination->create_links();
        $search['limit'] = array('persize' => $per_page, 'offset' => $this->pagination->get_cur_offset());
        $fields = array('goods_id', 'goods_name', 'goods_image');
        $data['goods'] = $this->goods_model->get_appoint_values($fields, null, $search);
        $data['goodsClass'] = $this->goods_class_model->get_by_pk($pid);
        $data['pid'] = $pid;

        $this->load->view($this->_site_path . '/goods/index', $data);
    }

    public function single($id = 0) {
        $data['info'] = $this->goods_model->get_by_pk($id);
        if (!$data['info']) {
            header('location:' . base_url($this->_site_path . '/index/index'));
            exit;
        }
        //更新浏览次数
        $this->goods_model->update_click($id);

        //显示上一条数据
        $data['pre'] = $this->goods_model->pre_goods($data['info']['gc_id'], $id);
        //显示下一条数据
        $data['next'] = $this->goods_model->next_goods($data['info']['gc_id'], $id);
        $this->load->view($this->_site_path . '/goods/single', $data);
    }

    //产品内容页
    public function content($id) {
        $data = array();
        $data['goods_data'] = $this->goods_model->get_by_pk($id);

        $data['recommend_data'] = $this->recommend();

        //更新浏览次数
        $this->goods_model->update_click($id);

        //显示上一条数据
        $data['pre'] = $this->goods_model->pre_goods($id);
        //显示下一条数据
        $data['next'] = $this->goods_model->next_goods($id);

        $this->get_banner(); //获取导航栏菜单
        $this->load->view_part($this->_site_path . '/goods/content', $data);
    }

    //推荐产品案例列表
    public function recommend() {
        $search = array(
            'attributes' => array(
                'goods_commend' => 1,
            ),
            'orders' => array(
                'created' => 'desc',
            ),
            'limit' => array(
                'persize' => 10,
            ),
        );
        return $this->goods_model->all($search);
    }

}
